<?php

/*
These functions are used to display and modify data about a given dataset
*/
function check_dataset_projectID_auth($tableMetadata, $showHTML = true){

    $output = true;
    //Needed for checking authorization
    $t_project_id = helper_get_current_project();
    if($t_project_id != 0 && ($t_project_id != $tableMetadata["issueProjectID"])){
        
        if($showHTML){
            require_once( 'project_api.php' );
            $actProjectName =  project_get_name($tableMetadata["issueProjectID"]);
            $host = "http://".$_SERVER['SERVER_NAME'];
            $redirectURL = $host."/plugin.php?page=DataRefine/data-doc-projects";
            $headerData = 'HTTP/1.1 403 Forbidden';
            header($headerData);
            html_page_top( "Data Documentation: Not Authorized" );
            echo "<div class=\"data-doc-page-body\">";
            echo "<div class=\"data-doc-page-primary\">";
            echo "<h2>Not Authorized to this Dataset</h2>";
            echo "<br/>";
            echo "<p>Sorry to give you some bad news! You need to switch to the <strong><em>".$actProjectName."</em></strong> issue-tracker project to work with this dataset.</p>";
            echo "<p>Either switch to the correct issue-tracker project via the pull-down menu (top right), ";
            echo "or go to the list of datasets monitored by the issue tracker: [<a href=\"".$redirectURL."\">Link</a>]</p>";
            echo "<br/>";
            echo "<br/>";
            echo "<br/>";
            echo "</div>";
            echo "</div>";
            html_page_bottom();
        }
        $output = false;  
    }
    return $output;
}//end function



function check_edit_resolution($tableMetaObj){
    //updates the metadata object based on changes to the status of the edit issue
    
    $tableMetadata = $tableMetaObj->currentMetadata;

    //now check on the status of the edit issue
    @$tpl_bug = bug_get( $tableMetadata["issueIDs"]["editIssue"], true );
    if($tpl_bug != false){
            if(!$tableMetadata["issueIDs"]["editResolved"] && $tpl_bug->status >= 80){
                    $tableMetaObj->mark_editIssue_resolution(true); //CHANGE the issue to now be resolved!
                    $tableMetaObj->add_descriptions();
                    $tableMetaObj->add_columns();
            }
            elseif($tableMetadata["issueIDs"]["editResolved"] && $tpl_bug->status < 80){
                    $tableMetaObj->mark_editIssue_resolution(false); //CHANGE the issue to now be NOT resolved!
            }
            else{
                    //do nothing, no discrency between the JSON metadata and the bug-tracker (database) resolution status
            }
    }
    
    return $tableMetaObj;
    
}//end function


function check_metadata_fieldDes_resolution($tableMetaObj){
    //updates the metadata object based on changes to the status of the edit issue
    
    $tableMetadata = $tableMetaObj->currentMetadata;
    
    $checkArray = array("describeIssue" => "describeResolved",
                        "fieldsIssue" => "fieldsResolved" );

    foreach($checkArray as $issueKey => $resolveField){
        //now check on the status of the issue
        @$tpl_bug = bug_get( $tableMetadata["issueIDs"][$issueKey], true );
        if($tpl_bug != false){
                if(!$tableMetadata["issueIDs"][$resolveField] && $tpl_bug->status >= 80){
                        $tableMetaObj->mark_fieldDes_resolution($resolveField, true); //CHANGE the issue to now be resolved!
                }
                elseif($tableMetadata["issueIDs"][$resolveField] && $tpl_bug->status < 80){
                        $tableMetaObj->mark_fieldDes_resolution($resolveField, false); //CHANGE the issue to now be NOT resolved!
                }
                else{
                        //do nothing, no discrency between the JSON metadata and the bug-tracker (database) resolution status
                }
        }
    }
    
    return $tableMetaObj;
    
}//end function


function check_otherIssues_resolution($tableMetaObj){
    //updates the metadata object based on changes to the status of the edit issue
    
    $tableMetadata = $tableMetaObj->currentMetadata;
    
    $otherIssues = $tableMetadata["issueIDs"]["others"];
    
    if(is_array($otherIssues)){
        foreach($otherIssues as $bugID => $statusArray){
            //now check on the status of the issue
            @$tpl_bug = bug_get( $bugID, true );
            if($tpl_bug != false){
                    if(!$otherIssues[$bugID]["resolved"] && $tpl_bug->status >= 80){
                            $tableMetaObj->mark_otherIssue_resolution($bugID, true); //CHANGE the issue to now be resolved!
                    }
                    elseif($otherIssues[$bugID]["resolved"] && $tpl_bug->status < 80){
                            $tableMetaObj->mark_otherIssue_resolution($bugID, false); //CHANGE the issue to now be NOT resolved!
                    }
                    else{
                            //do nothing, no discrency between the JSON metadata and the bug-tracker (database) resolution status
                    }
            }
        }
    }
    
    return $tableMetaObj;
    
}//end function



function primary_links($tableMetadata, $resolvedVal = " -RESOLVED-"){
    
    global $editLink;
    global $editResolved;
    
    global $describeLink;
    global $describeResolved;
    
    global $fieldsLink;
    global $fieldsResolved;
    
    //now prepare some handy links! This will be used to display links to issues related to this dataset 
    $editLink = "<a href='../view.php?id=".$tableMetadata["issueIDs"]["editIssue"]."'>Issue ".$tableMetadata["issueIDs"]["editIssue"]."</a>";
    if($tableMetadata["issueIDs"]["editResolved"]){
            $editResolved = $resolvedVal;
    }else{
            $editResolved = "";
    }
    
    
    $describeLink = "<a href='../view.php?id=".$tableMetadata["issueIDs"]["describeIssue"]."'>Issue ".$tableMetadata["issueIDs"]["describeIssue"]."</a>";
    if($tableMetadata["issueIDs"]["describeResolved"]){
            $describeResolved = $resolvedVal;
    }else{
            $describeResolved = "";
    }
    
    
    
    $fieldsLink = "<a href='../view.php?id=".$tableMetadata["issueIDs"]["fieldsIssue"]."'>Issue ".$tableMetadata["issueIDs"]["fieldsIssue"]."</a>";
    if($tableMetadata["issueIDs"]["fieldsResolved"]){
            $fieldsResolved = $resolvedVal;
    }else{
            $fieldsResolved  = "";
    }
    
       
}//end function



function other_links($tableMetadata, $noneOthers = "none"){
    
    global $otherIssues;
    
    $firstIssue = true;
    $otherIssues = $noneOthers;
    if(isset($tableMetadata["issueIDs"]["others"])){
	foreach($tableMetadata["issueIDs"]["others"] as $bugID => $statusArray){
		$outputLink = "<a href='../view.php?id=".$bugID."'>".$bugID."</a>";
                if($statusArray["resolved"]){
                    $outputLink = $outputLink." [RESOLVED]";
                }
                
		if($firstIssue){
			$firstIssue = false;
			$otherIssues = $outputLink;
		}
		else{
			$otherIssues .= ", ".$outputLink;
		}
	}
    }

}//end function



?>